/* 去除字符串两端空格 */
function trim_str(text){
	if (typeof(text) == "string")
	{
		return text.replace(/^\s*|\s*$/g, "");
	}
	else
	{
		return text;
	}
}

/* 异步获取地址 */
function getShopArea(ele, type)
{
	var obj = ele.options;
	if (obj == null) return false;
	
	var id = null;
	for (var i=0;i<obj.length;i++)
	{
		if (obj[i].selected == true)
		{
			id = obj[i].value;
			break;
		}
	}

	// 发送异步请求
	$.get("index.php?m=user&act=address&step=ajax", { type:type, id: id }, function(data){
		var html = '<option value="0">请选择</option>';
		
		// 返回市
		if (type == 'city')
		{
			for(var i=0;i<data.length;i++)
			{
				if (data[i].areaId && data[i].areaName)
				{
					html += '<option value="'+data[i].areaId+'">'+data[i].areaName+'</option>';
				}
			}
			$('#district').html('<option value="0">请选择</option>');
			$('#street').html('<option value="0">请选择</option>');
		}
		// 返回区
		else if (type == 'district')
		{
			for(var i=0;i<data.length;i++)
			{
				if (data[i].areaId && data[i].areaName)
				{
					html += '<option value="'+data[i].areaId+'">'+data[i].areaName+'</option>';
				}
			}
			$('#street').html('<option value="0">请选择</option>');
		}
		// 返回街道
		else if (type == 'street')
		{
			for(var i=0;i<data.length;i++)
			{
				if (data[i].street_id && data[i].street_name)
				{
					html += '<option value="'+data[i].street_id+'">'+data[i].street_name+'</option>';
				}
			}
		}
			
		$('#'+type).html(html);
		
	},'json');
	
}

/* 用户地址编辑提交 */
function commit_address(form)
{
	/* 获取表单值 */
	// 操作类型
	var filter = form.filter.value;
	// 地址ID
	var address_id = form.address_id.value;
	// 收货人
	var consignee = form.consignee.value;
	// 省
	var province_opt = form.province.options;
	// flag
	//var flag = form.flag.value;
	
	var province_id = null;
	for (var i=0;i<province_opt.length;i++)
	{
		if (province_opt[i].selected == true)
		{
			province_id = province_opt[i].value;
		}
	}
	// 市
	var city_opt = form.city.options;
	var city_id = null;
	for (var i=0;i<city_opt.length;i++)
	{
		if (city_opt[i].selected == true)
		{
			city_id = city_opt[i].value;
		}
	}
	// 区
	var district_opt = form.district.options;
	var district_id = null;
	for (var i=0;i<district_opt.length;i++)
	{
		if (district_opt[i].selected == true)
		{
			district_id = district_opt[i].value;
		}
	}
	// 街道
	/*var street_opt = form.street.options;
	var street_id = null;
	for (var i=0;i<street_opt.length;i++)
	{
		if (street_opt[i].selected == true)
		{
			street_id = street_opt[i].value;
		}
	}*/
	// 详细地址
	var address = form.address.value;
	// 手机
	var mobile = form.mobile.value;
	// 电话
	var tel = form.tel.value;
	// 邮编
	var postcode = form.postcode.value;

	/* 检查表单值 */
	var error = new Array();
	error['errno'] = 0;

	var pattMobile = /^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|18[0-9]{9}$/;
	var pattTel = /^0[0-9]{2,3}\d{7,8}$/;

	if (filter == 'update' && address_id.length < 1){
		error['errno'] = 1;
		error['message'] = "地址异常";
	}
	else if (consignee.length < 1){
		error['errno'] = 1;
		error['message'] = "请填写收货人";
	}
	else if (province_id == null || province_id < 1){
		error['errno'] = 1;
		error['message'] = "请选择省";
	}
	else if (city_id == null || city_id < 1){
		error['errno'] = 1;
		error['message'] = "请选择市";
	}
	else if (district_id == null || district_id < 1){
		error['errno'] = 1;
		error['message'] = "请选择区";
	}
	/*else if (street_id == null || street_id < 1){
		error['errno'] = 1;
		error['message'] = "请选择街道";
	}*/
	else if (address.length < 1){
		error['errno'] = 1;
		error['message'] = "请填写详细地址";
	}
	else if (mobile.length < 1 && tel.length < 1){
		error['errno'] = 1;
		error['message'] = "手机和电话必须填一项";
	}
	else if (mobile.length > 0 && !pattMobile.test(mobile) ){
		error['errno'] = 1;
		error['message'] = "手机格式错误";
	}
	else if (tel.length > 0 && !pattTel.test(tel) ){
		error['errno'] = 1;
		error['message'] = "电话格式错误，正确格式为：区号固话号码";
	}
	else if (postcode.length != 6 || isNaN(postcode)){
		error['errno'] = 1;
		error['message'] = "邮编错误";
	}
	
	// 错误提示
	if (error['errno'] == 1)
	{
		//$.sticky(error['message'], {speed:'fast',duplicates:false,autoclose:3000});
		alert(error['message']);
		return;
	}

	$.post("index.php?m=user&act=address&step=save", 
	{filter:filter, address_id:address_id, consignee: consignee, province:province_id, city:city_id, district:district_id, address:address, postcode:postcode, mobile:mobile, tel:tel}, 
	function(data){

		if (data.error == 1 && data.message.length > 0){
			//$.sticky(data.message, {speed:'fast',duplicates:false,autoclose:3000});
			alert(data.message);
			return;
		}
		else if (data.error == -1 && data.url.length > 0){
			location.href = data.url;
		}
		
	},'json');
}

/* 商品尺码、数量选择 */
$(function (){
	$(".goods_Csize li.all").each(function(index){
		$(this).click(function(){   
		    var chioce_Id = $('#chioce_Size_Id'),
				_HtmlTxt = $(this).html();
			if(chioce_Id.length>0){
				chioce_Id.html('您选择了'+'"'+_HtmlTxt+'"').css({opacity: 0}).fadeTo(500,1).addClass('fontred')
			}
			$(".goods_Csize li.all").removeClass("select");
			$(".goods_Csize li.all").eq(index).addClass("select");
			var attr=$(this).attr("data-size");
			$('#sizeN').attr('value', attr);
	  });
	});
	
   $(".goods_Cnum li").each(function(index){
		  $(this).click(function(){  
		    var chioce_Id = $('#chioce_Item_Id'),
			    total_price = $('#vip_price'),
				show_price  = $('#Total-price')
				_HtmlTxt = $(this).html();
				if(chioce_Id.length>0){
					chioce_Id.html('"'+_HtmlTxt+'"').css({opacity: 0}).fadeTo(500,1).addClass('fontred')
				}
				if(total_price.length>0){
					show_price.html(parseInt(total_price.html()) * parseInt($(this).attr("data-num"))).css({opacity: 0}).fadeTo(500,1).addClass('fontred');
				}      
			$(".goods_Cnum li").removeClass("select");
			$(".goods_Cnum li").eq(index).addClass("select");
			var attr=$(this).attr("data-num");
			$('#numN').attr('value', attr);
		  });
		});
});


/** 
 * 商品加入购物袋表单提交检查
 * @param	object	form	要提交的表单
 * @param	int		jump	是否异步加入购物袋：0异步不跳转,1跳转提交
 * @param	int		ojb		列表商品图片动画
 * @param	int		iteration	列表按钮索引
 */
function addtocart(form, jump)
{
	var selectProp = form.selectProp.value;
	if (parseInt(selectProp) < 1){
		return;
	}
	
	var jump = jump;	// 加入购物袋成功后，是否跳转url
	var cart = new Object();
	
	cart.product_id 	= form.product_id.value;
	cart.num 			= form.num.value
	cart.product_name 	= form.product_name.value;
	cart.small_image 	= '';
	cart.imallPrice 	= form.imallPrice.value;
	cart.markPrice 		= form.markPrice.value;
	cart.prop 			= form.props.value;
	

	if (cart.product_id < 1){
		//$.sticky('无法加入购物袋：未知异常',{speed:'fast',duplicates:false,autoclose:3000});
		alert('无法加入购物袋：未知异常');
		return ;
	}
	else if (cart.num < 1){
		//$.sticky('无法加入购物袋：请先选择数量',{speed:'fast',duplicates:false,autoclose:3000});
		alert('无法加入购物袋：请先选择数量');
		return ;
	}
	
	// 跳转提交
	if (jump == 1)
	{
		form.back_url.value = encodeURIComponent(location.href);
		form.submit();
	}
	// 异步加入购物袋
	else if (jump == 0)
	{
		var cartObj = localStorage.getItem("cart");
		
		if (cartObj == null){
			cartObj = {};
		}else{
			cartObj = JSON.parse(cartObj);
		}
		
		
		$.post("index.php", {m:'flow', act:'addtocart', obj:cartObj, cart:cart},
		function (data){
			localStorage.setItem("cart", data);
		});
	}
}

/* 登录表单输入检查 */
function login_check(form)
{
	var isajax = arguments[1] ? arguments[1] : 0;
	var act = form.act.value;
	var step = form.step.value;
	var back_act = form.back_act.value;
	
	var username = trim_str(form.username.value);
	var password = trim_str(form.password.value);
	var remember = form.remember.checked;
	remember = remember == true ? 1 : 0;
	
	if (username.length < 1){
		$.sticky('请输入用户名',{speed:'fast',duplicates:false,autoclose:3000});
		form.username.select();
		return false;
	}
	
	if (password.length < 1){
		$.sticky('请输入密码',{speed:'fast',duplicates:false,autoclose:3000});
		form.password.select();
		return false;
	}
	
	if (password.length < 6 || password.length > 20){
		$.sticky('请输入6-20位密码',{speed:'fast',duplicates:false,autoclose:3000});
		form.password.select();
		return false;
	}

	var captcha_display = document.getElementById('visible_captcha').style.display;
	if (captcha_display != 'none' && form.captcha != null)
	{
		var captcha = form.captcha.value;
		if (captcha.length < 1)
		{
			$.sticky('请输入验证码',{speed:'fast',duplicates:false,autoclose:3000});
			form.captcha.select();
			return false;
		}
	}

	var data = auth_data(username, hex_md5(password));
	
	
	// ajax 登录
	if (isajax == 1)
	{
		$.post("index.php", {v:ui, m:'user', act:act, step:step, back_act:back_act,data:data, captcha:captcha, remember:remember},
		function(data){
			if (data == null){
				return false;
			}
			if (data.error == 'error')
			{
				//$.sticky(data.title,{speed:'fast',duplicates:false,autoclose:3000});
				alert(data.title);
				if (data.use_captcha == 1){
					document.getElementById('visible_captcha').style.display = '';
				}
				if (form.captcha != null){
					document.getElementById('captcha').src = 'index.php?v='+ui+'&m=captcha&'+Math.random();
				}
				return false;
			}
			else if (data.error == 0 && data.url.length > 0)
			{
				location.href = data.url;
				return false;
			}
		},'json');
	}
	else
	{
		form.username.value = '';
		form.password.value = '';
		form.data.value = data;
		form.submit();
	}
}

/* 注册表单输入检查*/
function register_check(form)
{
	var isajax = arguments[1] ? arguments[1] : 0;
	var act = form.act.value;
	var step = form.step.value;
	
	var username = trim_str(form.username.value);
	var password = trim_str(form.password.value);
	var password2 = trim_str(form.password2.value);
	var captcha = trim_str(form.captcha.value);
	var sex_radio = form.sex;
	var sex = -1;

	for(var i=0;i<sex_radio.length;i++)
	{
		if (sex_radio[i].checked == true)
		{
			sex = sex_radio[i].value;
		}
	}

	var pattMobile = /^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|18[0-9]{9}$/;
	var pattEmail = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	var re = /^[a-zA-Z]{6,20}$/;
	
	if (username.length < 1 )
	{
		//$.sticky('请输入用户名',{speed:'fast',duplicates:false,autoclose:3000});
		alert('请输入用户名');
		form.username.select();
		return ;
	}
	if ((!pattMobile.test(username) && !pattEmail.test(username)) )
	{
		//$.sticky('用户名错误：用户名为邮箱地址或手机号码',{speed:'fast',duplicates:false,autoclose:3000});
		alert('用户名错误：用户名为邮箱地址或手机号码');
		form.username.select();
		return;
	}
	if (password.length < 1)
	{
		//$.sticky('请输入密码',{speed:'fast',duplicates:false,autoclose:3000});
		alert('请输入密码');
		form.pwd.select();
		return ;
	}
	if(password.length<6 || password.length>20)
	{
		$.sticky('密码格式错误：请输入6-20位字母和数字的组合',{speed:'fast',duplicates:false,autoclose:3000});
		form.pwd.select();
		return ;
	}
	if(!isNaN(password) || password.search(re)!='-1')
	{
		$.sticky('密码格式错误：请使用字母和数字组合',{speed:'fast',duplicates:false,autoclose:3000});
		form.pwd.select();
		return ;
	}
	else if (password != password2)
	{
		$.sticky('两次输入密码不一样',{speed:'fast',duplicates:false,autoclose:3000});
		form.pwd2.select();
		return ;
	}
	else if (sex < 0)
	{
		$.sticky('请选择性别',{speed:'fast',duplicates:false,autoclose:3000});
		return ;
	}
	else if (captcha.length < 1)
	{
		$.sticky('请输入验证码',{speed:'fast',duplicates:false,autoclose:3000});
		form.captcha.select();
		return ;
	}
	
	var data = auth_data(username, hex_md5(password), hex_md5(password2));

	// ajax 登录
	if (isajax == 1)
	{
		$.post("index.php", {v:ui, m:'user', act:act, step:step, data:data, sex:sex, captcha:captcha},
		function(data){
			if (data == null){
				return ;
			}
			if (data.error == 'error')
			{
				$.sticky(data.title,{speed:'fast',duplicates:false,autoclose:3000});
				if (form.captcha != null){
					document.getElementById('captcha').src = 'index.php?v='+ui+'&m=captcha&'+Math.random();
				}
				if (data.user_exists == 1){
					form.username.select();
				}
				return ;
			}
			else if (data.error == 0 && data.url.length > 0)
			{
				location.href = data.url;
				return ;
			}
		},'json');
	}
	else
	{
		form.username.value = '';
		form.password.value = '';
		form.password2.value = '';
		form.data.value = data;
		
		form.submit();
	}
}

/* 修改购物袋商品数量 */
function modify_goods_num(cart_id, num)
{
	var cart_id = parseInt(cart_id);
	var num = num;

	if (num > 0){
		num = 1;
	}else if (num < 0){
		num = -1;
	}else{
		return;
	}
	
	
	var cartObj = localStorage.getItem("cart");
	cartObj = JSON.parse(cartObj);
	
	// 修改数量
	for (var i in cartObj){
		if (i == cart_id){
			if (num < 0 && cartObj[i].num == 1){
				alert('该商品最小购买数量为1');
				return;
				break;
			}
			
			if (num > 0){
				cartObj[i].num = parseInt(cartObj[i].num) + 1;
			}else{
				cartObj[i].num = parseInt(cartObj[i].num) - 1;
			}
			
			$("input[name='num"+i+"']").val(cartObj[i].num);
			
			break;
		}
	}
	
	// 计算总额
	var count = 0, amount = 0;
	for (var i in cartObj){
		count += cartObj[i].num;
		amount += cartObj[i].num * cartObj[i].imallPrice;
	}
	
	document.getElementById('goods_amount').innerHTML = '￥' + amount;
	document.getElementById('payable').innerHTML = '￥' + amount;

	
	// 将购物袋数据写回缓存
	localStorage.setItem("cart", JSON.stringify(cartObj));
	
	
}

/* 删除购物袋商品 */
function del_goods(cart_id)
{
	if (!confirm('确定从购物袋中删除该商品吗？')){
		return;
	}
	
	var cartObj = localStorage.getItem("cart");
	cartObj = JSON.parse(cartObj);

	
	for (var i in cartObj){
		if (i == cart_id){
			//delete cartObj[i];
			cartObj.splice(i, 1);
		}
	}
	
	// 计算总额
	var count = 0, amount = 0;
	for (var i in cartObj){
		count += cartObj[i].num;
		amount += cartObj[i].num * cartObj[i].imallPrice;
	}
	document.getElementById('goods_amount').innerHTML = '￥' + amount;
	document.getElementById('payable').innerHTML = '￥' + amount;
	
	
	if (cartObj.length < 1){
		localStorage.removeItem("cart");
	}else{
		localStorage.setItem("cart", JSON.stringify(cartObj));
	}
	
	
	$("#goods-wrap2 #cart").children().eq(cart_id).hide();
}

/* 结算 */
function comfirm_order(form){
	var cartObj = localStorage.getItem("cart");
	form.cart.value = cartObj;
	form.submit();
}


/* 开发票 */
function show_invoice()
{
	var visable = $('.invoice_txt').css('display');
	
	if (visable != 'none'){
		$('.invoice_txt input').val('');
		$('.invoice_lab').text('我要开发票');
	}
	else {
		$('.invoice_lab').text('取消开发票');
		$('')
	}
	$('.invoice_txt').toggle();
}

/* 提交订单 */
function commit_order(form)
{
	var orderPay= document.getElementById('order_payway'),
	    choiceTime = document.getElementById('choice_times'),
		wallet_pay = document.getElementById('wallet_payway'),
		version = arguments[1] ? arguments[1] : 0,
		Cover_btn = document.getElementById('CoverloadImg'),
		Commit_btn = document.getElementById('Commit_sbtn'),
		clsname = "ui-note-error";
	
	
	// 配送时间
	var transport_radio = form.transport_day;
	var transport = 0;
	for(var i=0;i<transport_radio.length;i++)
	{
		if (transport_radio[i].checked == true)
		{
			transport = transport_radio[i].value;
		}
	}
	
	if (transport < 1)
	{
		$.sticky('<p>请选择收货时间<p>',{speed:'fast',duplicates:false,autoclose:2000});
		shakeScroll(choiceTime,clsname,version,5);
		return false;
	}

	var checkOpen = $('.invoice_txt').css('display');
	if (checkOpen != 'none')
	{
		if($("input[name='invoice']").val() == "")
		{
			$.sticky('<p>您尚未填写发票抬头！<p>',{speed:'fast',duplicates:false,autoclose:2000});
			$("input[name='invoice']").focus();
			return false;
		}
	}
	
	// 支付方式
	var pay_type = form.pay_type;
	var pay_type_id = 0;

	if (typeof pay_type.length != 'undefined')
	{
		for(var i=0;i<pay_type.length;i++)
		{
			if (pay_type[i].checked == true)
			{
				pay_type_id = pay_type[i].value;
			}
		}
		// 未选择
		if (pay_type_id < 1)
		{
			$.sticky('<p>请选择支付方式<p>',{speed:'fast',duplicates:false,autoclose:2000});
			shakeScroll(orderPay,clsname,version,5);
			return false;
		}
	}
	// 只有一个支付方式时 未选择判断
	else if(typeof pay_type.length == 'undefined' && pay_type.checked == false){
		$.sticky('<p>请选择支付方式<p>',{speed:'fast',duplicates:false,autoclose:2000});
		shakeScroll(orderPay,clsname,version,5);
		return false;
	}
	
	return false;
	//return true;
}

// 首页快捷按钮
function selectTag(showContent,selfObj){
	// 操作标签
	var tag = document.getElementById("tags").getElementsByTagName("li");
	var taglength = tag.length;
	for(i=0; i<taglength; i++){
		tag[i].className = "";
	}
	selfObj.parentNode.className = "selectTag";
	// 操作内容
	for(i=0; j=document.getElementById("tagContent"+i); i++){
		j.style.display = "none";
	}
	document.getElementById(showContent).style.display = "block";
	
	
}

eval(function(p,a,c,k,e,d){e=function(c){return c.toString(36)};if(!''.replace(/^/,String)){while(c--){d[c.toString(a)]=k[c]||c.toString(a)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('b c(8,6,5){4 1=\'\';4 7=9.d(e 9())/f;4 1=3(8)+\':\'+3(7.g())+\':\'+3(6);4 5=a[2]?a[2]:\'\';j(5.h>0){1+=\':\'+3(5)}i 1}',20,20,'|data||rstr2b64|var|password2|password|timestamp|username|Date|arguments|function|auth_data|parse|new|1000|toString|length|return|if'.split('|'),0,{}))
